package com.zst.bluetooth.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.ColorUtils;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zst.bluetooth.R;
import com.zst.bluetooth.bean.BlueMessage;

import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhang on 2016/3/18 0018. 消息适配器
 */
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {

    /**
     * 消息列表
     */
    private List<BlueMessage> list = new ArrayList<>();
    /**
     * 上下文对象
     */
    private Context mContext;
    /**
     * item中按钮点击事件监听
     */
    private OnItemBtnClickListener listener;

    /**
     * 消息适配器
     *
     * @param mContext 上下文对象
     * @param msgList  消息列表
     */
    public MsgAdapter(Context mContext, List<BlueMessage> msgList) {
        this.mContext = mContext;
        this.list = msgList;
    }

    /**
     * 创建viewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //使用RecycleView时一个相对固定的写法
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_msg, parent, false));
    }

    /**
     * 绑定ViewHolder,将数据绑定到ViewHolder上
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //获取到对应的消息
        BlueMessage message = list.get(position);
        //设置消息样式
        holder.setStyle(message);
        //设置消息内容
        holder.setContent(message.content);
        //设置发送者
        holder.setSender(message.sender);
        //设置时间
        holder.setTime(new DateTime(message.createTime).toString("YYYY/MM/dd HH:mm"));
    }

    @Override
    public int getItemCount() {
        //返回消息数量
        return list.size();
    }

    /**
     * 设置item按钮点击监听器
     *
     * @param listener 监听器
     */
    public void setListener(OnItemBtnClickListener listener) {
        this.listener = listener;
    }

    /**
     * item按钮点击监听器
     */
    public interface OnItemBtnClickListener {
        /**
         * 消息被点击
         *
         * @param msg 对应的消息
         */
        void onClick(BlueMessage msg);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private EditText content;
        private TextView sender;
        private TextView time;
        private CardView contentBackground;
        private ImageButton btn;
        private ProgressBar progressBar;
        private String ss = "发送到";

        public ViewHolder(View itemView) {
            super(itemView);
            //初始化itemUI
            content = (EditText) itemView.findViewById(R.id.content);
            sender = (TextView) itemView.findViewById(R.id.sender);
            time = (TextView) itemView.findViewById(R.id.time);
            contentBackground = (CardView) itemView.findViewById(R.id.content_card);
            btn = (ImageButton) itemView.findViewById(R.id.btn);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
            //为item上的按钮设置监听
            btn.setOnClickListener(this);

        }

        /**
         * 设置消息内容
         *
         * @param content 消息内容
         */
        public void setContent(String content) {
            this.content.setText(content);
        }

        /**
         * 设置发送者
         *
         * @param sender 发送者
         */
        public void setSender(String sender) {
            this.sender.setText(String.format("%s:%s", ss, sender));
        }

        /**
         * 设置时间
         *
         * @param time 时间
         */
        public void setTime(String time) {
            this.time.setText(time);
        }

        /**
         * 设置消息样式
         *
         * @param msg 消息
         */
        public void setStyle(BlueMessage msg) {
            //出事化UI状态:右侧控件都隐藏,消息内容不可编辑
            btn.setVisibility(View.GONE);
            progressBar.setVisibility(View.GONE);
            content.setEnabled(false);
            //根据消息状态进行对应item的UI样式设置
            switch (msg.state) {
                case STATE_PRE_SEND://发送前,显示发送按钮,背景为绿色,消息内容可以编辑
                    btn.setVisibility(View.VISIBLE);
                    btn.setImageResource(R.drawable.ic_content_send);
                    contentBackground.setCardBackgroundColor(Color.GREEN);
                    content.setEnabled(true);
                    break;
                case STATE_SENDING://发送中,显示进度条,消息内容不可编辑,背景为灰色
                    progressBar.setVisibility(View.VISIBLE);
                    contentBackground.setCardBackgroundColor(Color.DKGRAY);
                    break;
                case STATE_SEND_SUCCESS://发送成功,背景为蓝色
                    contentBackground.setCardBackgroundColor(mContext.getResources().getColor(R.color.blue));
                    break;
                case STATE_SEND_FAIL://发送失败,显示重发按钮,背景为红色
                    btn.setVisibility(View.VISIBLE);
                    btn.setImageResource(R.drawable.ic_content_re_send);
                    contentBackground.setCardBackgroundColor(Color.RED);
                    break;
            }
            Drawable drawable = mContext.getResources().getDrawable(R.drawable.ic_msg_task);
            switch (msg.type) {//根据消息类型设置图片,用来区分消息
                case TYPE_TASK:
                    drawable = mContext.getResources().getDrawable(R.drawable.ic_msg_task);
                    ss = "发送到";
                    break;
                case TYPE_SMS:
                    drawable = mContext.getResources().getDrawable(R.drawable.ic_msg_sms);
                    ss = "来自";
                    break;
            }
/// 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            content.setCompoundDrawables(drawable, null, null, null);
        }

        @Override
        public void onClick(View v) {
            if (v.equals(btn)) {
                if (null != listener) {//当按钮点击时,将点击事件传送给item按钮点击监听器
                    BlueMessage message = list.get(getAdapterPosition());
                    message.content = content.getText().toString();
                    listener.onClick(message);
                }
            }
        }
    }
}
